package com.ftg.elvis.service;

import com.ftg.elvis.Util.ExtractId;
import com.ftg.elvis.mapper.AdminUserMapper;
import com.ftg.elvis.pojo.Brand;
import com.ftg.elvis.pojo.Role;
import com.ftg.elvis.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class AdminUserService {

	@Autowired
	AdminUserMapper adminUserMapper;

	/**
	 * 查看用户名是否存在
	 * @param user
	 * @return
	 */
	public User findUserByUsername(User user){
		return adminUserMapper.findUserByUsername(user);
	}

	/**
	 * 注册
	 * @param user
	 * @return
	 */
	public int register(User user){
		user.setUid(UUID.randomUUID().toString());
		return adminUserMapper.register(user);
	}

	/**
	 * 登陆
	 * @param user
	 * @return
	 */
	public User login(User user){
		return adminUserMapper.login(user);
	}

	public int addUser(User user) {
		user.setUid(UUID.randomUUID().toString());
		return adminUserMapper.register(user);
	}

	public int updateUser(User user) {
		updateUserBrand(user);
		return adminUserMapper.updateUser(user);
	}
	private void updateUserBrand(User user){
		//接收到bid时
		if(!("".equals(user.getBid())) || null != user.getBid()){
			//该用户是否已经有品牌
			User pd = adminUserMapper.findBidByUsername(user.getUsername());
			//为空---添加
			if(pd == null){
				adminUserMapper.addUserBrand(user);
			}else{
				adminUserMapper.updateUserBrand(user);
			}
		}
	}

	public int deleteUser(String id) {
		adminUserMapper.deleteUser(id);
		adminUserMapper.deleteRole(id);
		return 1;
	}

	public List<User> showAll(int page, int limit, String username) {
		int begin = (page - 1) * limit;
		username = username == null ? "" : username;
		return adminUserMapper.showAll(begin, limit, username);
	}

	public int getCount(String username) {
		username = username == null ? "" : username;
		return adminUserMapper.getCount(username);
	}

	/**
	 * 显示所有角色
	 * @return
	 */
	public List<Role> showAllRole() {
		return adminUserMapper.showAllRole();
	}

	/**
	 * 显示我的角色
	 * @param uid
	 * @return
	 */
	public List<Role> showMyRole(String uid) {
		return adminUserMapper.showMyRole(uid);
	}

	/**
	 * 显示我没有的角色
	 * @param uid
	 * @return
	 */
	public List<Role> showNoRole(String uid) {
		return adminUserMapper.showNoRole(uid);
	}

	/**
	 * 修改某个用户的角色
	 * @param user_id
	 * @param role_ids
	 * @return
	 */
	@Transactional
	public int updateRoles(String user_id, String[] role_ids) {
		try{
			//定义集合，用于存储处理后的menu_id
			List<String> rids = new ArrayList<>();

			//删除该角色原有角色
			adminUserMapper.delMyRolesById(user_id);

			if(role_ids.length > 1) {
				//处理字符串数组
				for (int i = 0; i < role_ids.length; i++) {
					if (i % 2 == 0) {
						String menu_id = this.updateRole_id(role_ids[i]);
						rids.add(menu_id);
					} else {
						continue;
					}
				}

				//将新的权限逐一赋给该角色
				for (String role_id : rids) {
					adminUserMapper.addMyRoles(role_id, user_id);
				}
			}

			return 1;
		}catch (Exception e){
			e.printStackTrace();
			return 0;
		}

	}

	private String updateRole_id(String role_id) {
		//去掉[ { value ": "
		return ExtractId.extractId(role_id);
	}


	public List<Brand> showBrand() {
		return adminUserMapper.showBrand();
	}
}
